﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;


namespace BusinessLogic
{
    public static class ChiTietKhuyenMaiTangHang
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.ChiTietKhuyenMaiTangHang[] Select()
        {
            Entities.ChiTietKhuyenMaiTangHang[] list = null;
            try
            {
                string sql = Common.Constants.ChiTietKhuyenMaiTangHang.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietKhuyenMaiTangHang chitietkhuyenmaitanghang = new Entities.ChiTietKhuyenMaiTangHang();
                    chitietkhuyenmaitanghang.ChiTietKhuyenMaiTangHangID = int.Parse(dr[Common.Constants.ChiTietKhuyenMaiTangHang.ChiTietKhuyenMaiTangHangID].ToString());
                    chitietkhuyenmaitanghang.MaKhuyenMaiTangHang = dr[Common.Constants.ChiTietKhuyenMaiTangHang.MaKhuyenMaiTangHang].ToString();
                    chitietkhuyenmaitanghang.MaHangKhuyenMai = dr[Common.Constants.ChiTietKhuyenMaiTangHang.MaHangKhuyenMai].ToString();
                    chitietkhuyenmaitanghang.TenHangKhuyenMai = dr[Common.Constants.ChiTietKhuyenMaiTangHang.TenHangKhuyenMai].ToString();
                    chitietkhuyenmaitanghang.SoLuongKhuyenMai = int.Parse(dr[Common.Constants.ChiTietKhuyenMaiTangHang.SoLuongKhuyenMai].ToString());
                    chitietkhuyenmaitanghang.MaHangDuocKhuyenmai = dr[Common.Constants.ChiTietKhuyenMaiTangHang.MaHangDuocKhuyenmai].ToString();
                    chitietkhuyenmaitanghang.TenHangDuocKhuyenmai = dr[Common.Constants.ChiTietKhuyenMaiTangHang.TenHangDuocKhuyenmai].ToString();
                    chitietkhuyenmaitanghang.SoLuongDuocKhuyenMai = int.Parse(dr[Common.Constants.ChiTietKhuyenMaiTangHang.SoLuongDuocKhuyenMai].ToString());
                    arr.Add(chitietkhuyenmaitanghang);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.ChiTietKhuyenMaiTangHang[0];
                }
                else
                {
                    list = new Entities.ChiTietKhuyenMaiTangHang[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietKhuyenMaiTangHang)arr[i];
                    }
                }
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.ChiTietKhuyenMaiTangHang[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert update
        public static int InsertUpdate(Entities.ChiTietKhuyenMaiTangHang chitietkhuyenmaitanghang)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.ChiTietKhuyenMaiTangHang.InsertUpdate;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.HanhDong, SqlDbType.NVarChar, 50).Value = chitietkhuyenmaitanghang.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.ChiTietKhuyenMaiTangHangID, SqlDbType.Int).Value = chitietkhuyenmaitanghang.ChiTietKhuyenMaiTangHangID;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.MaKhuyenMaiTangHang, SqlDbType.VarChar, 50).Value = chitietkhuyenmaitanghang.MaKhuyenMaiTangHang;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.MaHangKhuyenMai, SqlDbType.VarChar, 50).Value = chitietkhuyenmaitanghang.MaHangKhuyenMai;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.TenHangKhuyenMai, SqlDbType.NVarChar, 200).Value = chitietkhuyenmaitanghang.TenHangKhuyenMai;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.SoLuongKhuyenMai, SqlDbType.Int).Value = chitietkhuyenmaitanghang.SoLuongKhuyenMai;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.MaHangDuocKhuyenmai, SqlDbType.NVarChar, 200).Value = chitietkhuyenmaitanghang.MaHangDuocKhuyenmai;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.TenHangDuocKhuyenmai, SqlDbType.NVarChar, 200).Value = chitietkhuyenmaitanghang.TenHangDuocKhuyenmai;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.SoLuongDuocKhuyenMai, SqlDbType.Int).Value = chitietkhuyenmaitanghang.SoLuongDuocKhuyenMai;
                rt = cmd.ExecuteNonQuery();
                cn.Close();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
        }

        public static int InsertUpdate(Entities.ChiTietKhuyenMaiTangHang[] chitietkhuyenmaitanghang)
        {
            try
            {
                foreach (Entities.ChiTietKhuyenMaiTangHang item in chitietkhuyenmaitanghang)
                {
                    if (InsertUpdate(item) == 0)
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        #endregion

        #region Delete
        public static int Delete(Entities.ChiTietKhuyenMaiTangHang chitietkhuyenmaitanghang)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.ChiTietKhuyenMaiTangHang.Delete;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietKhuyenMaiTangHang.MaKhuyenMaiTangHang, SqlDbType.VarChar, 50).Value = chitietkhuyenmaitanghang.MaKhuyenMaiTangHang;     
                rt = cmd.ExecuteNonQuery();
                cn.Close();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
        }
        #endregion
    }
}
#endregion